<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>后台管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="__STATIC__/res/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="__STATIC__/res/adminui/dist/css/admin.css" media="all">
    <style>
        .layui-sort {
            line-height: 28px;
            height: 28px;
        }
        .layui-line {
            display: inline-block;
            border-left:1px solid #c7c7c7;
            height: 30px;
            line-height: 38px;
            margin: 0 6px;
            vertical-align:middle;
        }
    </style>
</head>
<body>

<div class="layui-fluid" {if $list} style="padding-top: 0"{/if}>
    <div class="layui-card">
        <div class="layui-card-header">
            <div class="layui-col-xs11 layui layui-col-sm11 layui-col-md11 layui-col-lg11">
                {$category['title']} - 文档列表
            </div>
            <div class="layui-col-xs1 layui-col-md1 layui-col-lg1">&nbsp;
                <a href="javascript:window.location.reload(true);" title="刷新">
                    <i class="layui-icon layui-icon-refresh-3"></i>
                </a>
            </div>
        </div>
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">文档标签</label>
                    <div class="layui-input-inline">
                        <select name="flag_id">
                            <option value="0">所有标签</option>
                            {foreach $flags as $row}
                            <option value="{$row['code']}">{$row['name']}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">标题</label>
                    <div class="layui-input-inline">
                        <input type="text" name="title" placeholder="标题" autocomplete="off" class="layui-input">
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-inline">
                        <select name="status">
                            <option value="0">全部</option>
                            <option value="1">已发布</option>
                            <option value="2">未发布</option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    {if in_array('article-query',$actions)}
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-contlist-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    {/if}
                </div>
            </div>
        </div>

        <div class="layui-card-body">
            <div style="padding-bottom: 10px;" class="layui-form">
                {if in_array('article-add',$actions)}
                <button class="layui-btn layuiadmin-btn-list" data-type="add">添加</button>
                {/if}
                {if in_array('article-sort',$actions)}
                <button class="layui-btn layuiadmin-btn-list" data-type="batchSort">排序</button>
                {/if}
                {if in_array('article-batch-del',$actions)}
                <button class="layui-btn layui-btn-danger layuiadmin-btn-list" data-type="batchDel">删除</button>
                {/if}
                <span class="layui-line layui-hide-xs"></span>
                {if in_array('article-batch-check',$actions)}
                <button class="layui-btn layui-hide-xs layuiadmin-btn-list" data-type="check">审核</button>
                <button class="layui-btn layui-hide-xs layuiadmin-btn-list" data-type="cancelCheck">取消审核</button>
                {/if}
                {if in_array('article-batch-flag',$actions)}
                <span class="layui-line layui-hide-xs"></span>
                <button class="layui-btn layui-hide-xs layuiadmin-btn-list" data-type="setFlag">设置</button>
                <button class="layui-btn layui-hide-xs layuiadmin-btn-list" data-type="cancelFlag">取消设置</button>
                <div class="layui-inline layui-hide-xs">
                    &nbsp;
                    <div class="layui-input-inline">
                        <select name="flag_id" id="flag_id">
                            {foreach $flags as $row}
                            <option value="{$row['id']}">{$row['name']}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                {/if}
            </div>
            <table id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
            <script type="text/html" id="titleTpl">
              {{  d.title }} {{# if (d.flag_name!=='') { }}<span style="color:#FF5722">[{{  d.flag_name }}]</span>{{# } }}
            </script>
            <script type="text/html" id="sortTpl">
                <input type="number" value="{{ d.sort }}"  class="layui-input layui-sort sort_{{ d.id }}" autocomplete="off"/>
            </script>
            <script type="text/html" id="buttonTpl">
                {{#  if(d.status == '1'){ }}
                <button class="layui-btn layui-btn-xs">已发布</button>
                {{#  } else { }}
                <button class="layui-btn layui-btn-danger layui-btn-xs">未发布</button>
                {{#  } }}
            </script>
            <script type="text/html" id="table-content-list">
                {if in_array('article-edit',$actions)}
                <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                {/if}
                {if in_array('article-del',$actions)}
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                {/if}
            </script>
        </div>
    </div>
</div>

<script>
    var ARTICLE_LIST_URL='{:url("article/list",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_ARTICLED_URL='{:url("article/add",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_EDIT_URL='{:url("article/edit",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_DELETE_URL='{:url("article/doDelete",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_DO_SORT_URL='{:url("article/doSort",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_DO_FLAG_URL='{:url("article/doFlag",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_DO_CHECK_URL='{:url("article/doCheck",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
    var ARTICLE_BATCH_DELETE_URL='{:url("article/batchDoDelete",["website_code"=>$websiteCode,"category_id"=>$categoryId])}';
</script>
<script src="__STATIC__/res/layui/layui.js"></script>
<script>
    layui.config({
        base: '__STATIC__/res/' //静态资源所在路径
    }).use(['index', 'article', 'table'], function(){
        var table = layui.table
            ,form = layui.form;

        //监听搜索
        form.on('submit(LAY-app-contlist-search)', function(data){
            var field = data.field;

            //执行重载
            table.reload('LAY-app-content-list', {
                where: field
            });
        });

        var $ = layui.$, active = {
            batchDel: function(){
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    ,checkData = checkStatus.data; //得到选中的数据

                if(checkData.length === 0){
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });

                layer.confirm('确定删除吗？', function(index) {

                    //执行 Ajax 后重载
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(",")},
                        url: ARTICLE_BATCH_DELETE_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                                return
                            }

                            layer.msg(res.result.msg, {
                                offset: '15px'
                                , icon: 1
                                , time: 1000
                            });

                            table.reload('LAY-app-content-list');
                            layer.msg('已删除');
                        }
                    });
                });
            },
            add: function(){
                location.href = ARTICLE_ARTICLED_URL
            },
            batchSort: function () {
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });

                var sort = []
                $(checkData).each(function (index, item) {
                    sort.push($(".sort_"+item.id).val())
                });

                layer.confirm('确定排序吗？', function (index) {
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(","),"sort":sort.join(",")},
                        url: ARTICLE_DO_SORT_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                                return;
                            }

                            //执行 Ajax 后重载
                            table.reload('LAY-app-content-list');
                            layer.msg('已排序');
                        }
                    })

                });
            },
            cancelCheck: function () {
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });

                layer.confirm('确定取消审核吗？', function (index) {
                    console.log(ids)
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(","),'status':2},
                        url: ARTICLE_DO_CHECK_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                                return
                            }

                            //执行 Ajax 后重载
                            table.reload('LAY-app-content-list');
                            layer.msg('已取消审核');
                        }
                    })


                });
            },
            check: function () {
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });

                layer.confirm('确定审核吗？', function (index) {
                    console.log(ids)
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(","),'status':1},
                        url: ARTICLE_DO_CHECK_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                                return;
                            }

                            //执行 Ajax 后重载
                            table.reload('LAY-app-content-list');
                            layer.msg('已审核');
                        }
                    })


                });
            },
            cancelFlag: function () {
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });
                var flagId =  $('#flag_id').val();
                console.log('flag_id',flagId)
                var flagName = $('#flag_id').find('option:selected').text()

                layer.confirm('确定取消设置'+flagName+'吗？', function (index) {
                    console.log(ids)
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(","),'flag_id':flagId,'operate':2},
                        url: ARTICLE_DO_FLAG_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });

                                return
                            }

                            //执行 Ajax 后重载
                            table.reload('LAY-app-content-list');
                            layer.msg('已取消设置'+flagName);
                        }
                    })


                });
            },
            setFlag: function () {
                var checkStatus = table.checkStatus('LAY-app-content-list')
                    , checkData = checkStatus.data; //得到选中的数据

                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }

                var ids = []
                $(checkData).each(function (index, item) {
                    ids.push(item.id)
                });
                var flagId =  $('#flag_id').find('option:selected').val();
                console.log('flag_id',flagId)
                var flagName = $('#flag_id').find('option:selected').text()

                layer.confirm('确定设置'+flagName+'吗？', function (index) {
                    console.log(ids)
                    $.ajax({
                        method: 'post',
                        data: {'ids': ids.join(","),'flag_id':flagId,'operate':1},
                        url: ARTICLE_DO_FLAG_URL,
                        success: function (res) {
                            if (res.result.status) {
                                layer.msg(res.result.msg, {
                                    offset: '15px'
                                    , icon: 2
                                    , time: 1000
                                });
                                return
                            }

                            //执行 Ajax 后重载
                            table.reload('LAY-app-content-list');
                            layer.msg('已设置'+flagName);
                        }
                    })

                });
            }
        };

        $('.layui-btn.layuiadmin-btn-list').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>
</body>
</html>
